﻿using Microsoft.Graphics.Canvas.Effects;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using 纸片人.AttributeUsageFloder;
using 纸片人.InfoDataFolder;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace 纸片人.GpuPage
{
    [LastModified("2022-6-12", "create Mosaic.xaml")]
    [PageType("gpu", "马赛克效果")]
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MosaicPage : Page
    {
        public MosaicPage()
        {
            this.InitializeComponent();
            Init();
        }
        LayerBitmapData bitmapData = new LayerBitmapData();

        private void Init()
        {
            selectPicture.Click += async (s, e) => { await bitmapData.SelectOneBitmapAsync(); canvas.Invalidate(); };
            canvas.CreateResources += (s, e) =>
            {
                e.TrackAsyncAction(bitmapData.LoadOneShadeCodeAsync(Shaders.ShaderType.Mosaic).AsAsyncAction());
            };
            canvas.Draw += (s, e) =>
            {
                Function.DrawGridGraphics(e.DrawingSession);

                if (bitmapData.oneBitmap != null && bitmapData.oneShaderCode != null)
                {
                    var element = (FrameworkElement)s;
                    float effectWidth = (float)element.ActualWidth;
                    float effectHeight = (float)element.ActualHeight * 0.7f;
                    float previewWidth = effectWidth;
                    float previewHeight = (float)element.ActualHeight * 0.3f;


                    var previewTran = Function.CalcutateImageCenteredTransform(previewWidth, previewHeight, bitmapData.oneWidth, bitmapData.oneHeight);
                    previewTran.Source = bitmapData.oneBitmap;
                    e.DrawingSession.DrawImage(previewTran, 0, effectHeight);


                    PixelShaderEffect effect = new PixelShaderEffect(bitmapData.oneShaderCode)
                    {
                        Source1 = bitmapData.oneBitmap,
                        Properties = { ["size"] = (float)threshold.Value }
                    };

                    var smin=(float)(1/ threshold.Value);
                    var smax=(float)(1* threshold.Value);
                    var scale = new ScaleEffect()
                    {
                        Source = new ScaleEffect()
                        {
                            Source = bitmapData.oneBitmap,
                            Scale = new System.Numerics.Vector2(smin, smin),
                            InterpolationMode = Microsoft.Graphics.Canvas.CanvasImageInterpolation.NearestNeighbor
                        },
                        Scale = new System.Numerics.Vector2(smax, smax),
                         InterpolationMode = Microsoft.Graphics.Canvas.CanvasImageInterpolation.NearestNeighbor
                    };

                    var effectTran = Function.CalcutateImageCenteredTransform(effectWidth, effectHeight, bitmapData.oneWidth, bitmapData.oneHeight);
                    effectTran.Source = scale;
                    e.DrawingSession.DrawImage(effectTran);
                }
            };
            threshold.ValueChanged += (s, e) =>
            {
                canvas.Invalidate();
            };
        }
    }
}
